﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Futaba.SBus.Telemetry.Configuration;
using System.IO;
using Futaba.SBus.Extensions;
using System.Linq;
using Futaba.SBus.Telemetry;

namespace Futaba.SBus.Tests.Telemetry
{
	[TestClass]
	public class SlotFactoryTests : BaseTest
	{

		[TestMethod, TestCategory("SBUS2")]
		[DeploymentItem(@"Samples\TxData\config.FLI")]
		public void ParseFli()
		{
			/*
				0;0;0;1;1;3;3;5;6;7;16;24;24;24;24;24;24;
				RECEIVER;RECEIVER;RECEIVER;VOLTAGE SENSOR;VOLTAGE SENSOR;VOLTAGE SENSOR;VOLTAGE SENSOR;TEMPERATURE F1713 ;TEMPERATURE F1713 ;TEMPERATURE F1713 ;RPM SENSOR;GPS F1675  ;GPS F1675  ;GPS F1675  ;GPS F1675  ;GPS F1675  ;GPS F1675  ;
				BATTERY;EXTERNAL BATTERY;STATUS;BATTERY;EXT. BATT.;BATTERY;EXT. BATT.;TEMPERATURE;TEMPERATURE;TEMPERATURE;ROTATION;DISTANCE;SPEED;ALTITUDE;VARIOMETER;LATITUDE;LONGITUDE;
			 * */
			var target = new SlotFactory();
			//using (var stream = GetType().Assembly.GetManifestResourceStream("Futaba.Sbus.Tests.Samples.TxData.config.FLI"))
			using (var stream = File.OpenRead(@"config.FLI"))
			{
				var slots = target.ParseFliFile(stream);
				Assert.IsNotNull(slots);
				Assert.AreEqual(17, slots.Length);
				Assert.IsNotNull(slots.Single(s => s.SensorType == SensorType.RpmSensor && s.SlotId == SlotIdType.Slot16));
				Assert.IsNotNull(slots.Single(s => s.SensorType == SensorType.GpsSensor && s.SlotId == SlotIdType.Slot24));
				foreach (var slot in slots)
				{
					TestContext.WriteLine(slot.ToString());
				}
			}
		}

		[TestMethod, TestCategory("SBUS2")]
		[DeploymentItem(@"Samples\slots.xml")]
		public void LoadSlots()
		{
			var target = new SlotFactory();
			var sensors = new SensorFactory().GetKnownSensors();
			Slot[] slots;
			using (var stream = File.OpenRead("slots.xml"))
			{
				slots = target.Load(stream, sensors);
			}
			foreach (var s in slots)
			{
				TestContext.WriteLine("{0}", s);
			}
			Assert.IsNotNull(slots);
			Assert.AreEqual(17, slots.Length);
			Assert.IsFalse(slots.Any(s => s.Sensor == null));
			Assert.IsFalse(slots.Any(s => s.Parameter == null));
		}

		[TestMethod, TestCategory("SBUS2"), TestCategory("GenerateConfig")]
		[DeploymentItem(@"Samples\TxData\config.FLI")]
		public void SaveSlots()
		{
			var target = new SlotFactory();
			ISlot[] slots;
			using (var stream = File.OpenRead(@"config.FLI"))
			{
				slots = target.ParseFliFile(stream);
				Assert.IsNotNull(slots);
				Assert.AreEqual(17, slots.Length);
				Assert.IsNotNull(slots.Single(s => s.SensorType == SensorType.RpmSensor && s.SlotId == SlotIdType.Slot16));
				Assert.IsNotNull(slots.Single(s => s.SensorType == SensorType.GpsSensor && s.SlotId == SlotIdType.Slot24));
			}

			var outFile = Path.Combine(TestContext.TestResultsDirectory, "slots.xml");
			using (var stream = File.OpenWrite(outFile))
			{
				target.Save(slots.Cast<Slot>().ToArray(), stream);
			}
			Assert.IsTrue(File.Exists(outFile));
			Assert.AreNotEqual(0, new FileInfo(outFile).Length);
			TestContext.AddResultFile(outFile);
		}

		[TestMethod, TestCategory("SBUS2"), TestCategory("GenerateConfig")]
		[DeploymentItem(@"Samples\TBoxData\flight1.FLI")]
		public void SaveSlots_TboxFlight1()
		{
			var target = new SlotFactory();
			ISlot[] slots;
			using (var stream = File.OpenRead(@"flight1.FLI"))
			{
				slots = target.ParseFliFile(stream);
				Assert.IsNotNull(slots);
				Assert.AreEqual(17, slots.Length);
				Assert.IsNotNull(slots.Single(s => s.SensorType == SensorType.RpmSensor && s.SlotId == SlotIdType.Slot16));
				Assert.IsNotNull(slots.Single(s => s.SensorType == SensorType.GpsSensor && s.SlotId == SlotIdType.Slot24));
			}

			var outFile = Path.Combine(TestContext.TestResultsDirectory, "flight1.xml");
			using (var stream = File.OpenWrite(outFile))
			{
				target.Save(slots.Cast<Slot>().ToArray(), stream);
			}
			Assert.IsTrue(File.Exists(outFile));
			Assert.AreNotEqual(0, new FileInfo(outFile).Length);
			TestContext.AddResultFile(outFile);
		}

		[TestMethod, TestCategory("SBUS2"), TestCategory("GenerateConfig")]
		[DeploymentItem(@"Samples\TBoxData\flight2.FLI")]
		public void SaveSlots_TboxFlight2()
		{
			var target = new SlotFactory();
			ISlot[] slots;
			using (var stream = File.OpenRead(@"flight2.FLI"))
			{
				slots = target.ParseFliFile(stream);
				Assert.IsNotNull(slots);
				Assert.AreEqual(24, slots.Length);
				Assert.IsNotNull(slots.Single(s => s.SensorType == SensorType.RpmSensor && s.SlotId == SlotIdType.Slot16));
				Assert.IsNotNull(slots.Single(s => s.SensorType == SensorType.GpsSensor && s.SlotId == SlotIdType.Slot24));
			}

			var outFile = Path.Combine(TestContext.TestResultsDirectory, "flight2.xml");
			using (var stream = File.OpenWrite(outFile))
			{
				target.Save(slots.Cast<Slot>().ToArray(), stream);
			}
			Assert.IsTrue(File.Exists(outFile));
			Assert.AreNotEqual(0, new FileInfo(outFile).Length);
			TestContext.AddResultFile(outFile);
		}

	}
}
